package com.seanzx.dp.condition;

import com.seanzx.dp.entity.Column;
import com.seanzx.dp.memory.TableBuffer;

import java.util.ArrayList;
import java.util.List;

public class ConditionChain {

    private final List<Condition> conditions = new ArrayList<>();

    public boolean test(String databaseName, String tableName, List<String> row) {
        for (Condition condition : conditions) {
            String columnName = condition.getColumnName();
            Column column = TableBuffer.getColumn(databaseName, tableName, columnName);
            String value = row.get(column.getIndex());
            if (!condition.test(value)) {
                return false;
            }
        }
        return true;
    }

    public void add(String columnName, ConditionType conditionType, List<String> values) {
        Condition condition = new Condition();
        condition.setColumnName(columnName);
        condition.setConditionType(conditionType);
        condition.setValues(values);
        conditions.add(condition);
    }

    public void add(Condition condition) {
        conditions.add(condition);
    }

}
